package com.yboot.starter.id.core;

import cn.hutool.core.lang.Singleton;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;

/**
 * 雪花算法ID生成工具类
 * 必须保证单例，否则会导致ID重复
 */
public class SnowflakeUtil {
    private static volatile Snowflake snowflake;
    
    /**
     * 获取默认配置的雪花算法实例
     */
    public static Snowflake getInstance() {
        if (snowflake == null) {
            synchronized (SnowflakeUtil.class) {
                if (snowflake == null) {
                    snowflake = IdUtil.getSnowflake(31, 31);
                }
            }
        }
        return snowflake;
    }
    
    /**
     * 获取指定workerId和datacenterId的雪花算法实例
     */
    public static Snowflake getInstance(long workerId, long datacenterId) {
        return getInstance(workerId, datacenterId);
    }
    
    /**
     * 生成下一个ID
     */
    public static long nextId() {
        return getInstance().nextId();
    }
    
    /**
     * 生成下一个ID字符串
     */
    public static String nextIdStr() {
        return getInstance().nextIdStr();
    }

}